package com.xdf.data_transmitter.utils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class TestAsync {
    @Autowired
    private TestAsyncBTask asyncBTask;

    public void methodA() throws InterruptedException {
        System.out.println("A开始：" + System.currentTimeMillis());
        Thread.sleep(1000);
        System.out.println("A结束：" + System.currentTimeMillis());
    }

    @Async
    public void methodBAsync() throws InterruptedException {
        System.out.println("异步方法内部开始：" + System.currentTimeMillis());
        Thread.sleep(3000);
        System.out.println("异步方法内部结束：" + System.currentTimeMillis());
    }

    public void methodB() throws InterruptedException {
        System.out.println("同步方法内部开始：" + System.currentTimeMillis());
        Thread.sleep(3000);
        System.out.println("同步方法内部结束：" + System.currentTimeMillis());
    }

    public void methodC() throws InterruptedException {
        System.out.println("C开始：" + System.currentTimeMillis());
        Thread.sleep(1000);
        System.out.println("C结束：" + System.currentTimeMillis());
    }

    public void goSync() throws InterruptedException {// 执行同步方法
        methodA();
        methodB();
        methodC();
    }


    public void goAsync() throws InterruptedException {// 执行异步方法
        methodA();
//        methodBAsync();
        asyncBTask.methodBAsync();
        methodC();
    }
}
